<?php
class PostSettingModel extends Model {
	protected $autoCheckFields = false;
	/**
	 * 地区列表
	 * 
	 * @return Ambigous <boolean, array>
	 */
	public function getDistrict() {
		$districts = F ( 'PostSetting/district' );
		if (empty ( $districts )) {
			$districts = M ( 'district' )->where ( 'level=1' )->select ();
			F ( 'PostSetting/district', $districts );
		}
		return $districts;
	}
	/**
	 * 快递公司列表
	 * $show 1:启用的 0:所有
	 * @return array
	 */
	public function getCompanys($show = '0') {
		$where = $show ? 'status=1' : '';
		$companys = M ( 'company' )->where ( $where )->order ( ' status DESC ' )->select ();
		return $companys;
	}
	/**
	 * 模板列表
	 * $group 1:分组取基础设置的模板 2：视图下模板所有行
	 * 
	 */
	public function getTemplates( $group = '0' ) {
		$group = $group ? 'id' : '';
		//D('PosttemplateView')->group( $group )->select();
		return D('PosttemplateView')->group( $group )->select();
	}
	public function viewCompanys() {
		$companys = $this->getCompanys ( 1 );
		$out = '';
		foreach ( $companys as $company ) {
			$out .= "<option value='{$company['id']}'>{$company['name']}</option>";
		}
		return $out;
	}
	public function viewDisctrict( $cityid='0' ) {
		$district = $this->getDistrict ();
		$out = '';
		foreach ( $district as $dist ) {
			$selected = ( $cityid == $dist['id'] ) ? ' selected="selected"' : '';
			$out .= '<option value="' . $dist ['id'] . '"'.$selected.'>' . $dist ['name'] . '</option>';
		}
		return $out;
	}
	
	public function getDiscrictName( $did ) {
		return M ( 'district' )->where ( "id={$did}" )->getField('name');
	}
}